<template>
  <div class="my-container">
    <el-card class="box-card">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :span="24">
          <el-col :span="12">
            <el-form-item label="编码分段" prop="codeSegment">
              <el-select v-model="form.codeSegment" clearable placeholder="请选择编码分段">
                <el-option
                  v-for="item in codeSegments"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="段间分隔符" prop="separator">
              <el-select
                v-model="form.separator"
                placeholder="请选择段间分隔符"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="dict in dict.type.encode_separator"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :span="24">
          <el-col :span="12">
            <el-form-item label="应用规则" prop="applicationRule">
              <el-select v-model="form.applicationRule" clearable placeholder="请选择应用规则" >
                <el-option
                  v-for="item in applicationRules"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <div class="tab-title">
            编码一段
          </div>
          <el-form-item label="编码一段" prop="encode1">
            <el-select v-model="form.encode1" clearable placeholder="选择编码一段" >
              <el-option
                v-for="item in encodeType1"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设置值" prop="constRecord" v-if="form.encode1 === '01'">
            <el-input v-model="form.constRecord" placeholder="请设置值" />
          </el-form-item>
          <el-form-item label="日期格式" prop="dateFormat" v-if="form.encode1 === '02'">
            <el-input v-model="form.dateFormat" placeholder="请选择日期格式" />
          </el-form-item>
        </el-row>
        <el-row>
          <div class="tab-title">
            编码二段
          </div>
          <el-form-item label="编码二段" prop="encode2">
            <el-select v-model="form.encode2" clearable placeholder="选择编码二段" v-if="form.codeSegment === 3">
              <el-option
                v-for="item in encodeType1"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled">
              </el-option>
            </el-select>
            <el-select v-model="form.encode2" clearable placeholder="选择编码二段" v-else>
              <el-option
                label="流水号"
                value="03">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设置值" prop="constRecord" v-if="form.encode2 === '01'">
            <el-input v-model="form.constRecord" placeholder="请设置值" />
          </el-form-item>
          <el-form-item label="日期格式" prop="dateFormat" v-if="form.encode2 === '02'">
            <el-select
              v-model="form.dateFormat"
              placeholder="请选择日期格式"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.rule_date_format"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="流水号长度" prop="serialNumLength" v-if="form.encode2 === '03'">
            <el-input v-model="form.serialNumLength" placeholder="请选择流水号长度" />
          </el-form-item>
        </el-row>
        <el-row  v-if="form.codeSegment && form.codeSegment > 2">
          <div class="tab-title">
            编码三段
          </div>
          <el-form-item label="编码三段" prop="encode3">
            <el-select v-model="form.encode3" clearable placeholder="选择编码三段" >
              <el-option
                v-for="item in encodeType2"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设置值" prop="constRecord" v-if="form.encode3 === '01'">
            <el-input v-model="form.constRecord" placeholder="请设置值" />
          </el-form-item>
          <el-form-item label="日期格式" prop="dateFormat" v-if="form.encode3 === '02'">
            <el-select
              v-model="form.dateFormat"
              placeholder="请选择日期格式"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.rule_date_format"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="流水号长度" prop="serialNumLength" v-if="form.encode3 === '03'">
            <el-select v-model="form.serialNumLength" clearable  placeholder="请选择流水号长度">
              <el-option
                v-for="number in numbersToDisplay"
                :key="number"
                :label="number"
                :value="number"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>
      </el-form>
      <div class="dialog-footer" style="display: flex;
    justify-content: center;">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { listRules, addRules, updateRules } from "@/api/basicData/ruleSetting/vehicleIdGen.js";

export default {
  name: "VehicleIdGenPage",
  dicts: ['rule_date_format', 'encode_separator'],
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      codeSegments: [
        {
          label: '2',
          value: 2
        },
        {
          label: '3',
          value: 3
        }
      ],
      separators: [
        {
          label: '-',
          value: '01',
        },
        {
          label: '_',
          value: '02',
        },
        {
          label: '无',
          value: '03',
        },
      ],
      applicationRules: [
        {
          label: '新增显示且允许修改',
          value: '01',
        },
        {
          label: '新增显示且不允许修改',
          value: '02',
        }
      ],
      encodeType1: [
        {
          label: '常量',
          value: '01',
          disabled: false
        },
        {
          label: '单据日期',
          value: '02',
          disabled: false
        }
      ],
      encodeType2: [
        {
          label: '常量',
          value: '01',
          disabled: false
        },
        {
          label: '单据日期',
          value: '02',
          disabled: false
        },
        {
          label: '流水号',
          value: '03',
          disabled: false
        }
      ],
      numbersToDisplay: [3, 4, 5],
      // 表单参数
      form: {
        id: null,
        codeSegment: 3,
        separator: '03',
        applicationRule: '01',
        encode1: null,
        encode2: null,
        encode3: null,
        constRecord: null,
        dateFormat: null,
        serialNumLength: 5
      },
      edit: true,
      // 表单校验
      rules: {
        codeSegment: [
          { required: true, message: "编码分段不能为空", trigger: "blur" }
        ],
        separator: [
          { required: true, message: "段间分割不能为空", trigger: "blur" }
        ],
        applicationRule: [
          { required: true, message: "应用分段不能为空", trigger: "blur" }
        ],
        encode1: [
          { required: true, message: "编码分段一不能为空", trigger: "blur" }
        ],
        encode2: [
          { required: true, message: "编码分段二不能为空", trigger: "blur" }
        ],
        encode3: [
          { required: true, message: "编码分段三不能为空", trigger: "blur" }
        ],
        constRecord: [
          { required: true, message: "常量不能为空", trigger: "blur" }
        ],
        dateFormat: [
          { required: true, message: "日期格式不能为空", trigger: "blur" }
        ],
        serialNumLength: [
          { required: true, message: "流水号长度不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch: {
    show: {
      handler(newVal, oldVal) {
        if(newVal) {
          this.getList()
        }
      }
    },
    'form.codeSegment': {
      immediate: true,
      handler(newVal, oldVal) {
        if(!this.edit){
          this.edit = !this.edit
          return
        }
        this.form.encode1 = null
        this.form.encode2 = null
        this.form.encode3 = null
        this.form.constRecord = null
        this.form.dateFormat = null
        this.form.serialNumLength = null
      }
    },
    'form.encode1': {
      immediate: true,
      handler(newVal, oldVal) {
        this.checkEncode(newVal, oldVal)
      }
    },
    'form.encode2': {
      immediate: true,
      handler(newVal, oldVal) {
        this.checkEncode(newVal, oldVal)
      }
    },
    'form.encode3': {
      immediate: true,
      handler(newVal, oldVal) {
        this.checkEncode(newVal, oldVal)
      }
    }
  },

  created() {
    this.getList();
  },
  methods: {
    checkEncode(newVal, oldVal) {
      if(!this.edit){
        this.edit = !this.edit
        return
      }
      if(oldVal === '01') {
        this.form.constRecord = null
      } else if(oldVal === '02') {
        this.form.dateFormat = null
      }else if(oldVal === '03') {
        this.form.serialNumLength = null
      }

      // 遍历数组找到包含指定值的对象
      for (let i = 0; i < 2; i++) {
        if (newVal && this.encodeType1[i].value === newVal) {
          // 修改找到的对象的属性值
          this.$set(this.encodeType1[i], 'disabled', true)
          this.$set(this.encodeType2[i], 'disabled', true)
          break; // 找到后可以跳出循环
        }
        if (oldVal && this.encodeType1[i].value === oldVal) {
          this.$set(this.encodeType1[i], 'disabled', false)
          this.$set(this.encodeType2[i], 'disabled', false)
        }
      }
      if (newVal && this.encodeType2[2].value === newVal) {
        // 修改找到的对象的属性值
        this.$set(this.encodeType2[2], 'disabled', true)
      }
      if (oldVal && this.encodeType2[2].value === oldVal) {
        this.$set(this.encodeType2[2], 'disabled', false)
      }
    },
    /** 查询车辆ID规则列表 */
    getList() {
      this.edit = false
      this.loading = true;
      listRules(this.queryParams).then(response => {
        let data = response.rows[0]
        let encodeJoinSequenceString = (data || {}).encodeJoinSequence
        Object.assign(this.form, {}, data)

        // 使用 split 方法将字符串拆分成数组
        const encodeJoinSequenceArray = (encodeJoinSequenceString || '').split(',');

        for (let index = 0; index < encodeJoinSequenceArray.length; index++) {
          let field = 'encode' + (index + 1);
          this.$set(this.form, field, encodeJoinSequenceArray[index]);
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        codeSegment: 3,
        separator: '03',
        applicationRule: '01',
        encode1: null,
        encode2: null,
        encode3: null,
        constRecord: null,
        dateFormat: null,
        serialNumLength: 5
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let encodeJoinSequenceString = ''
          if(this.form.encode1) {
            encodeJoinSequenceString += this.form.encode1 + ","
          }
          if(this.form.encode2) {
            encodeJoinSequenceString += this.form.encode2 + ","
          }
          if(this.form.encode3) {
            encodeJoinSequenceString += this.form.encode3 + ","
          }
          encodeJoinSequenceString = encodeJoinSequenceString
                  .substring(0,encodeJoinSequenceString.length-1)
          this.form.encodeJoinSequence = encodeJoinSequenceString
          if (this.form.id != null) {
            updateRules(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRules(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
<style>
.my-container {
  display: flex;
  justify-content: center;
}
</style>